package com.lufei.message_system.Controller;

import com.lufei.message_system.Data.PageResult;
import com.lufei.message_system.Data.Result;
import com.lufei.message_system.Data.UserVD.*;
import com.lufei.message_system.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;

    // 创建用户
    @PostMapping
    public Result createUser(@RequestBody UserCreateDTO userDTO) {
        return userService.createUser(userDTO);
    }

    // 获取用户详情
    @GetMapping("/{userId}")
    public Result getUser(@PathVariable String userId) {
        return userService.getUser(userId);
    }

    // 更新用户信息
    @PutMapping("/{userId}")
    public Result updateUser(@PathVariable String userId,
                             @RequestBody UserUpdateDTO userupdateDTO) {
        return userService.updateUser(userId, userupdateDTO);
    }

    // 删除用户
    @DeleteMapping("/{userId}")
    public Result deleteUser(@PathVariable String userId) {
        return userService.deleteUser(userId);
    }

    // 用户登录
    @PostMapping("/checkLogin")
    public Result<UserVO> checkLogin(@RequestBody UserLoginDTO loginDTO) {
        return userService.checklogin(loginDTO);
    }

    // 分页查询用户
    @GetMapping
    public Result<PageResult<UserVO>> listUsers(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer size,
                                                UserQueryDTO queryDTO) {
        return userService.listUsers(page, size, queryDTO);
    }

    // 根据教师ID查询学生列表（带分页）
    @GetMapping("/teachers/{userId}/students")
    public Result<PageResult<UserVO>> listUsersByTeacherId(
            @PathVariable String userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return userService.listUsersByTeacherId(userId, page, size);
    }

    // 重置密码
    @PostMapping("/{userId}/password/reset")
    public Result resetPassword(@PathVariable String userId,
                                @RequestBody PasswordResetDTO resetDTO) {
        return userService.resetPassword(userId, resetDTO);
    }
}